﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DayuCode.Designer.Common
{
    public class Cast
    {
        public static int ToInt32(string str, int defaultValue)
        {
            if (str == null || str.Length == 0) return defaultValue;
            str = str.Trim();
            int num;
            if (int.TryParse(str, out num))
            {
                return num;
            }
            else return defaultValue;
        }

        public static int ToInt32(object obj, int defaultValue)
        {
            if (obj == null) return default;
            if (obj is int) return (int)obj;
            return ToInt32(obj.ToString(), defaultValue);
        }

        public static long ToLong(string str, long defaultValue)
        {
            if (str == null || str.Length == 0) return defaultValue;
            str = str.Trim();
            long num;
            if (long.TryParse(str, out num))
            {
                return num;
            }
            else return defaultValue;
        }

        public static long ToLong(object obj, long defaultValue)
        {
            if (obj == null) return default;
            if (obj is long) return (long)obj;
            return ToLong(obj.ToString(), defaultValue);
        }
    }
}
